package demo2;

public class Test {
    public static void drawshapes2(){
        Rect rect = new Rect();
        Cycle cycle = new Cycle();
        Flower flower = new Flower();
        //Shape[] shapes = {new Rect(),new Cycle(),new Flower(),new Rect()};
        Shape[] shapes = {rect,cycle,flower,cycle,rect};
        for(Shape shape:shapes){  //使用for-each遍历数组for（元素类型 变量名：数组名）
            shape.draw();  //通过父类的引用调用重写的方法
        }
    }
    public static void drawshapes(){
        Rect rect = new Rect();
        Cycle cycle = new Cycle();
        Flower flower = new Flower();
        String[] shapes = {"cycle","rect","flower","cycle","rect"};
        for (int i = 0; i <shapes.length ; i++) {
            String shape = shapes[i];
            if (shape.equals("cycle")){  //定义了一个String类型的shape变量
                cycle.draw();
            }else if (shape.equals("rect")){
                rect.draw();
            } else if (shape.equals(flower)) {
                flower.draw();
            }
        }
    }

    public static void main(String[] args) {
       // drawshapes();
        drawshapes2();
    }
}
